package com.jyy.prefabricated.service;

import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.entity.ManageRule;
import com.jyy.prefabricated.enums.BaseStatusEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.ManageRuleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class ManageRuleService {

    @Autowired
    private ManageRuleMapper manageRuleMapper;


    /**
     * 新增
     * @param manageRule
     * @return
     */
    public int insert(ManageRule manageRule){
        manageRule.setStatus(BaseStatusEnum.INVALID.getValue());
        manageRuleMapper.insert(manageRule);
        return manageRule.getId();
    }


    /**
     * 更新
     * @param manageRule
     * @return
     */
    public int update(ManageRule manageRule){
        if(manageRule.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        return manageRuleMapper.updateByPrimaryKeyWithBLOBs(manageRule);
    }

    /**
     * 批量更新状态
     * @param dto
     * @return
     */
    public int batchUpdateStatus(BatchDto dto){
        if(dto.getIds().size() == 0){
            throw new CustomException("参数id不能为空");
        }
        return manageRuleMapper.batchUpdateStatus(dto.getIds(), dto.getStatus());
    }

    /**
     * 批量删除
     * @param dto
     * @return
     */
    public int batchDelete(BatchDto dto){
        if(dto.getIds().size() == 0){
            throw new CustomException("参数id不能为空");
        }
        return manageRuleMapper.batchDelete(dto.getIds());
    }


    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        return manageRuleMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public ManageRule selectById(Integer id){
        return manageRuleMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据参数查询
     * @param categoryId 分类id
     * @param title 标题
     * @param status 状态
     * @return
     */
    public List<ManageRule> selectByParams(Integer categoryId, String title, Integer status){
        return manageRuleMapper.selectByParams(categoryId, title, status);
    }

    /**
     * 查询上一条或下一条数据
     * @param id 当前id
     * @param type 类型
     * @return
     */
    public List<ManageRule> selectByPage(Integer id, String type){
        return manageRuleMapper.selectByPage(id, BaseStatusEnum.VALID.getValue(), type);
    }
}
